package com.yihang.springboot_yihang.controller;

import com.yihang.springboot_yihang.pojo.ResponseMessage;
import com.yihang.springboot_yihang.pojo.User;
import com.yihang.springboot_yihang.pojo.dto.UserDto;
import com.yihang.springboot_yihang.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController//接口返回方法
@RequestMapping("/user")
public class Usercontroller {

    @Autowired
    IUserService userService;

    //增加
    //@PostMapping("/add")
    @PostMapping
    public ResponseMessage add(@Validated @RequestBody UserDto user) {
        User userAdd = userService.add(user);
        return ResponseMessage.success(userAdd);
    }


    //查询
    //@GetMapping("/query")
    @GetMapping("/{userId}")  //URL:localhost:8080/user/1  method:get
    public ResponseMessage add(@PathVariable Integer userId) {
        User userNew = userService.getUser(userId);
        return ResponseMessage.success(userNew);


    }


    //修改
    //@PutMapping("/update")
    @PutMapping
    public ResponseMessage edit(@Validated @PathVariable UserDto user) {
        User userNew = userService.edit(user);
        return ResponseMessage.success(userNew);




    }





    //删除
    //@DeleteMapping("/delete")
    public ResponseMessage delete(@PathVariable Integer userId) {
        userService.delete(userId);
        return ResponseMessage.success();






}


}